<template>
	<view class="administrator">
		<view class="administrator-card" >
			<!-- <uni-card :is-shadow="false" :border="false" :spacing="'20rpx 40rpx'" :padding="0" >
				<uni-forms ref="valiForm" :recruits="rules" :modelValue="valiFormData" labelWidth="80">
					<uni-forms-item label="联系电话" required name="phone">
						<uni-easyinput :clearable="false" :focus="true" type="number" :inputBorder="false" v-model="valiFormData.phone" placeholder="请输入联系方式" trim="both" />
					</uni-forms-item>
				</uni-forms>
			</uni-card> -->
			<view class="administrator-item">
				<view class="administrator-title">联系电话</view>
				<view class="administrator-small">
					<input type="number" v-model="valiFormData.phone" placeholder="请输入联系方式">
				</view>
			</view>
			<view class="administrator-item">
				<view class="administrator-title">管理门店</view>
				<view class="administrator-small" @tap="isZhi = true" v-if="valiFormData.hotel_id">{{hotel_name}}</view>
				<view class="administrator-small" @tap="isZhi = true" v-else>选择门店(可多选)</view>
			</view>
		</view>
		<view class="administrator-but">
			<button class="but but1" plain @click="deleClick()" v-if="id">删除</button>
			<button class="but" plain @click="addClick('valiForm')">保存</button>
		</view>
		<view class="zhi-box" v-show="isZhi">
			<view style="flex: 1;" @tap="isZhi = false"></view>
			<view class="zhi-small">
				<checkbox-group @change="checkboxChange">
					<label class="uni-list-cell uni-list-cell-pd zhi-item" v-for="item in HoList" :key="item.id">
						<view>
							<checkbox :value="item.id" :checked="item.checked" />
						</view>
						<view>{{item.hotel_name}}</view>
					</label>
				</checkbox-group>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isZhi: false,
				HoList: [],
				valiFormData:{
					phone: '',
					hotel_id: ''
				},
				hotel_name: '',
				id: '',
				hotel_ids: []
			};
		},
		onLoad(options) {
			if(options){
				this.id = options.id
				this.valiFormData.phone = options.phone
				this.valiFormData.hotel_id = options.hotel_ids
				this.hotel_ids = options.hotel_ids.split(',')
			}
		},
		onShow() {
			this.getHolist()
		},
		methods:{
			getHolist(){
				const params = {
					
				}
				this.$http.post('/index/Hotelpersonal/hotellist', params).then(res => {
					if(res.length==0){
						uni.showToast({
							title: '请新增门店',
							icon: 'none'
						})
						setTimeout(function(){
							uni.navigateBack({
								delta: 1
							})
						},1000)
						return
					}
					this.HoList = res
					let array = []
					for (let i in this.HoList) {
						for (let j in this.hotel_ids) {
							if(this.hotel_ids[j]==this.HoList[i].id){
								array.push(this.HoList[i].hotel_name)
							}
						}
					}
					this.hotel_name = array.join("  ")
					for (let i in this.HoList) {
						for (let j in this.hotel_ids) {
							if(this.hotel_ids[i]==this.HoList[i].id){
								this.HoList[i].checked = true
							}else{
								this.HoList[i].checked = false
							}
						}
					}
				}).catch(res => {
					uni.hideLoading()
					uni.showToast({
						title: res.errMsg,
						icon: "none"
					})
				})
			},
			checkboxChange(e){
				let array = []
				let arr = e.detail.value
				for (let i in this.HoList) {
					for (let j in arr) {
						if(arr[j]==this.HoList[i].id){
							array.push(this.HoList[i].hotel_name)
						}
					}
				}
				console.log(e.detail.value,array)
				this.hotel_name = array.join("  ")
				this.valiFormData.hotel_id = e.detail.value.toString()
			},
			/**
			 * 删除按钮
			 * @param {number} i 当前列表的id
			 */
			async deleClick() {
				try {
					let res = await this.$http.post('/index/Hotelpersonal/del_admin',{id:this.id})
					uni.showToast({
						title: '删除成功',
						icon:'none',
					});
					setTimeout(function(){
						uni.navigateBack({
						    delta: 1
						});
					},1000)
				}catch (e) {
					uni.showToast({
						title: e.errMsg,
						icon:"none"
					})
				}
			},
			/** 添加联系人事件
			 * @param {Object} ref 添加联系人的电话数据
			 */
			async addClick(ref) {
				const params = this.valiFormData
				this.$http.post('/index/Hotelpersonal/add_admin', params).then(res => {
					uni.navigateBack({
					    delta: 1
					});
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						icon: "none"
					})
				})
			}
		}
	}
</script>

<style lang="scss">
.administrator {
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	height: 100vh;
	.input-placeholder {
		font-size: 28rpx;
		color: rgba($color: #000000, $alpha: 0.5);
	}
	.administrator-card {
		width: 690rpx;
		border-radius: 13rpx;
		overflow: hidden;
		margin: 0 auto;
		margin-top: 40rpx;
		.uni-card {
			border-radius: 20rpx;
			overflow: visible;
			.uni-card__header {
				padding: 6rpx 0;
			}
			.uni-forms-item__inner {
				padding: 0;
			}
			.label-text {
				font-size: 28rpx;
				font-weight: 500;
				color: rgba($color: #000000, $alpha: 0.7);
			}
			.uni-easyinput__content-input, .uni-combox__input {
				text-align: right;
			}
		}
	}
	.administrator-but {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		height: 80rpx;
		margin-bottom: 40rpx;
		.but {
			flex: 1;
			color: #FFFFFF;
			background: linear-gradient(-90deg, #8dc9f1, #d8eefc);
			border-radius: 40rpx;
			line-height: 80rpx;
			border: 0;
			margin: 0 20rpx;
		}
		.but1 {
			color: #646464;
			border: 2rpx solid #8C8C8C;
			background: linear-gradient(-90deg, #ffffff, #ffffff);
		}
	}
}
.zhi-box{
	background-color: rgba(0,0,0,0.5);
	position: fixed;
	z-index: 9999;
	width: 100%;
	height: 100vh;
	top: 0;
	left: 0;
	display: flex;
	.zhi-small{
		position: absolute;
		bottom: 0;
		left: 0;
		width: 100%;
		z-index: 9999999;
		height: 600rpx;
		background-color: #FFFFFF;
		border-radius: 13rpx 13rpx 0 0;
		padding: 30rpx;
		overflow: auto;
	}
	.zhi-item{
		margin-bottom: 60rpx;
		display: flex;
	}
}
.administrator-item{
	width: 690rpx;
	margin: 0 auto;
	display: flex;
	justify-content: space-between;
	padding: 20rpx 40rpx;
	background-color: #fff;
	.administrator-title{
		width: 5em;
		font-size: 32rpx;
	}
	.administrator-small{
		flex: 1;
		text-align: right;
		color: #afafaf;
		font-size: 32rpx;
	}
}
</style>
